//
//  BKGDEvaluationNormalCell.m
//  BESTKEEP
//
//  Created by 毛韶谦 on 2016/10/10.
//  Copyright © 2016年 YISHANG. All rights reserved.
//

#import "BKGDEvaluationNormalCell.h"
#import "BKGDImagesView.h"
#import "BKGDGradeView.h"
#import "WUImageBrowseView.h"
#import "WUCopyLabel.h"

#define BKGDEvaluationPraiseAlreadyImage [UIImage imageNamed:@"BKGDPraiseAlready"]
#define BKGDEvaluationPraiseImage [UIImage imageNamed:@"BKGDPraise"]

@interface BKGDEvaluationNormalCell()<BKGDImagesViewDelegate,WUImageBrowseViewDelegate>

@property(nonatomic,strong) UIImageView *avatarImageView;
@property(nonatomic,strong) UILabel *nicknameLabel;
@property(nonatomic,strong) UILabel *dateLabel;
//@property(nonatomic,strong) WUCopyLabel *contentLabel;
@property(nonatomic,strong) UILabel *contentLabel;
@property(nonatomic,strong) BKGDImagesView *imagesView;
@property(nonatomic,strong) BKGDGradeView *gradeView;
@property(nonatomic,strong) UILabel *propertiesLabel;
@property(nonatomic,strong) UILabel *praiseLabel;

@property(nonatomic,strong) CALayer *topLineLayer;

@property(nonatomic,assign) BOOL isVote;//是否已经点赞

@end

@implementation BKGDEvaluationNormalCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        [self initialize];
    }
    return self;
}

-(void)initialize {
    
    _topLineLayer = [[CALayer alloc] init];
    _topLineLayer.backgroundColor = [UIColor colorWithRed:0.937 green:0.937 blue:0.957 alpha:1.00].CGColor;
    [self.contentView.layer addSublayer:_topLineLayer];
    
    _avatarImageView = [[UIImageView alloc] init];
    _avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    _avatarImageView.clipsToBounds = YES;
    _avatarImageView.layer.masksToBounds = YES;
    _avatarImageView.layer.cornerRadius = 17;
    [self.contentView addSubview:_avatarImageView];
    
    _nicknameLabel = [[UILabel alloc] init];
    _nicknameLabel.font = [UIFont boldSystemFontOfSize:14];
    _nicknameLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.00];
    _nicknameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_nicknameLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = [UIFont systemFontOfSize:12];
    _dateLabel.textColor = [UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1.00];
    _dateLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_dateLabel];
    
    _gradeView = [[BKGDGradeView alloc] init];
    _gradeView.allowSelection = NO;
    [self.contentView addSubview:_gradeView];
    
//    _contentLabel = [[WUCopyLabel alloc] init];
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:13];
    _contentLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.00];
    _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self.contentView addSubview:_contentLabel];
    
    _imagesView = [[BKGDImagesView alloc] initWithAutoLayout:NO];
    _imagesView.delegate = self;
    _imagesView.hidden = YES;
    [self.contentView addSubview:_imagesView];
    
    _propertiesLabel = [[UILabel alloc] init];
    _propertiesLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _propertiesLabel.font = [UIFont systemFontOfSize:12];
    _propertiesLabel.textColor = [UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1.00];
    [self.contentView addSubview:_propertiesLabel];
    
    _praiseLabel = [[UILabel alloc] init];
    _praiseLabel.textColor = [UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1.00];
    _praiseLabel.font = [UIFont systemFontOfSize:12];
    _praiseLabel.textAlignment = NSTextAlignmentRight;
    _praiseLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:_praiseLabel];
    
    [self makeConstraints];
    
    UITapGestureRecognizer *praiseLabelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(praiseTap:)];
    praiseLabelTap.numberOfTapsRequired = 1;
    [_praiseLabel addGestureRecognizer:praiseLabelTap];
}

-(void)makeConstraints {
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(12);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarImageView.mas_right).offset(8);
        make.centerY.equalTo(_avatarImageView);
        make.right.lessThanOrEqualTo(self.mas_centerX).offset(-10);
    }];
    
    [_gradeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nicknameLabel.mas_right).offset(5);
        make.centerY.equalTo(_nicknameLabel);
        make.size.mas_equalTo(CGSizeMake(80, 20));
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_gradeView);
        make.right.mas_equalTo(-12);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatarImageView.mas_bottom).offset(10);
        make.left.equalTo(self.mas_left).offset(12);
        make.right.equalTo(self.mas_right).offset(-12);
    }];
    
    [_imagesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(1);
        make.right.mas_equalTo(-1);
        make.height.mas_equalTo(64);
    }];
    
    [_praiseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-12);
        make.right.mas_equalTo(-12);
        make.width.mas_lessThanOrEqualTo(80);
    }];
    
    [_propertiesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_contentLabel);
        make.right.equalTo(_praiseLabel.mas_left).offset(-10);
        make.bottom.mas_equalTo(-15);
    }];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _topLineLayer.frame = CGRectMake(0, CGRectGetHeight(self.frame) - 1, CGRectGetWidth(self.frame), 1);
}

-(void)fillData:(BKGDEvaluationObject *)evaluation {
    if(!evaluation) {
        return;
    }
    
    if(!evaluation.userName) {
        evaluation.userName = @"***";
    }
    
    [_avatarImageView load:evaluation.userPhoto placeholderImage:[UIImage imageNamed:@"BKGDAvatarDefault"]];
    
    _nicknameLabel.text = evaluation.userName;
    
    
    _dateLabel.text = [evaluation.createdDateStr substringWithRange:NSMakeRange(0, 10)];
    _gradeView.gradeValue = evaluation.scoringGrades;
    
//    _contentLabel.text = evaluation.experiences;
    
    NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:evaluation.experiences];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 6;
    [contentStr addAttributes:@{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName: HexColor(0x666666)} range:NSMakeRange(0, contentStr.string.length)];
    _contentLabel.attributedText = contentStr;
    
    _propertiesLabel.text = [self getPropsString:evaluation];
    
    _isVote = evaluation.isVote;
    
    _praiseLabel.attributedText = [self getPraiseAttributedString:evaluation.helpfulnessNum];
    
    if(evaluation.images && evaluation.images.count > 0) {
        NSMutableArray<NSString*> *images = [NSMutableArray array];
        for (NSDictionary<NSString*, NSString* > *item in evaluation.images) {
            [images addObject:item[@"image"]];
        }
        
        _imagesView.images = images;
        _imagesView.hidden = NO;
    } else {
        _imagesView.images = nil;
        _imagesView.hidden = YES;
    }
}

/**
 获取显示的商品参数
 */
-(NSString*)getPropsString:(BKGDEvaluationObject*)evaluation {
    
    NSArray<BKGDEvaluPropertyObject*> *props = evaluation.propRelStrs;
    
    if(!props || props.count == 0) {
        return nil;
    }
    
    NSMutableString *str = [NSMutableString string];
    
    for (BKGDEvaluPropertyObject *prop in props) {
        [str appendFormat:@"%@:%@  ", prop.proName, prop.proValue];
    }
    
    return [NSString stringWithString:str];
}

-(NSAttributedString*)getPraiseAttributedString:(NSInteger)count {
    NSString *string = [NSString stringWithFormat:@" %ld", (long)count];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    if(_isVote) {
        textAttachment.image = BKGDEvaluationPraiseAlreadyImage;
    } else {
        textAttachment.image = BKGDEvaluationPraiseImage;
    }
    textAttachment.bounds = CGRectMake(0, -5, 20, 20);
    
    [attString insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:0];
    
    return attString;
}

+(CGFloat)computeHeight:(BKGDEvaluationObject*)evaluation {
    
    CGFloat height = 54;//固定高度
    
    NSString *string = evaluation.experiences;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 6;
    CGSize size = CGSizeMake([[UIScreen mainScreen] bounds].size.width - 40, CGFLOAT_MAX);
    CGSize contentSize = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSParagraphStyleAttributeName: paragraphStyle} context:nil].size;
    height += contentSize.height;
    
    height += 10;
    
    if(evaluation.images && evaluation.images.count > 0) {
        height += 64;
        height += 15;
    }
    
    height += 12;
    height += 15;
    
    return height;
}

#pragma -mark imagesView delegate

-(void)imagesView:(BKGDImagesView *)view didSelectAtIndex:(NSInteger)index atImage:(UIImage *)image {
    
    if(!image) {
        return;
    }
    
    WUImageBrowseView *ibView = [[WUImageBrowseView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ibView.delegate = self;
    ibView.currentPage = index;
    ibView.images = view.images;
    ibView.pageControlStyle = WUPageControlStyleUILabel;
    
    UIWindow *backView = [[UIApplication sharedApplication] keyWindow];
    
    CGRect startFrame = [view rectToScreenAtItemIndex:index];
    
    [ibView show:backView startFrame:startFrame foregroundImage:image];
    
//    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
}

-(void)showImageWithIndex:(NSInteger)index {
    if(_imagesView.images && _imagesView.images.count > 0) {
        WUImageBrowseView *ibView = [[WUImageBrowseView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        ibView.delegate = self;
        ibView.currentPage = index;
        ibView.images = _imagesView.images;
        ibView.pageControlStyle = WUPageControlStyleUILabel;
        
        UIWindow *backView = [[UIApplication sharedApplication] keyWindow];
        
        [ibView show:backView];
    }
}

#pragma -mark WUImageBrowser delegate

-(CGRect)imageBrowseView:(WUImageBrowseView *)view willCloseAtIndex:(NSInteger)index {
    
//    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    CGRect rect = [_imagesView rectToScreenAtItemIndex:index];
    
    return rect;
}

#pragma -mark rect

-(void)imageItemRect:(CGRect *)rect image:(UIImage *__autoreleasing *)image index:(NSInteger *)index atPoint:(CGPoint)point {
    
    point = [self convertPoint:point toView:_imagesView];
    
    NSDictionary *dict = [_imagesView propertyWithPoint:point];
    if(!dict) {
        *rect = CGRectZero;
        *index = 0;
        *image = nil;
        return;
    }
    
    *rect = [dict[BKGDImagesViewItemScreenRect] CGRectValue];
    *image = dict[BKGDImagesViewItemImage];
    *index = [dict[BKGDImagesViewItemIndex] integerValue];
}

#pragma -mark 点赞

-(void)praiseTap:(UIGestureRecognizer*)recognizer {
    if(_delegate) {
        [_delegate evaluationNormalCellPraiseAtCell:self isVote:_isVote];
    }
}

@end
